package com.example.easybuy.controller;

import com.alibaba.fastjson.JSONObject;
import com.example.easybuy.pojo.User;
import com.example.easybuy.pojo.Useraddress;
import com.example.easybuy.service.UseraddressService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class UseraddressController {

    private Logger logger=Logger.getLogger(getClass());

    @Resource
    private UseraddressService useraddressService;

    @Resource
    private RedisTemplate redisTemplate;

    /*
    添加收货地址
     */
    @RequestMapping("addUserAddress")
    @ResponseBody
    @CrossOrigin
    public void addUserAddress(Useraddress useraddress){
        logger.debug("UseraddressController-addUserAddress()-useraddress:"+useraddress);
        //拿到存放在redis里面的登录用户
        String user=(String)redisTemplate.opsForValue().get("user");
        //把user Json字符串转换为user对象
        User userObj = JSONObject.parseObject(user, User.class);
        System.out.println("UseraddressController-addUserAddress()-userObj:"+userObj);
        //用户账号id
        int userId=userObj.getId();
        System.out.println("UseraddressController-addUserAddress()-userId:"+userId);
        //收货地址创建时间
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String nowTime=df.format(new Date());
        System.out.println("UseraddressController-addUserAddress()-nowTime:"+nowTime);
        useraddress.setCreateTime(nowTime);
        useraddress.setUserId(userId);
        String success=useraddressService.addUserAddress(useraddress);
        logger.debug("UseraddressController-addUserAddress()-success:"+success);
    }

    /*
    根据 登录用户id 查询收货地址
     */
    @RequestMapping("getUserAddressList")
    @ResponseBody
    @CrossOrigin
    public List<Useraddress> getUserAddressList(){
        logger.debug("UseraddressController-getUserAddressList()-...start...");
        //拿到存放在redis里面的登录用户
        String user=(String)redisTemplate.opsForValue().get("user");
        //把user Json字符串转换为user对象
        User userObj = JSONObject.parseObject(user, User.class);
        logger.debug("UseraddressController-getUserAddressList()-userObj:"+userObj);
        //用户账号id
        int userId=userObj.getId();
        logger.debug("UseraddressController-getUserAddressList()-userId:"+userId);
        List<Useraddress> useraddressList=useraddressService.getUserAddressList(userId);
        logger.debug("UseraddressController-getUserAddressList()-useraddressList："+useraddressList);
        return useraddressList;
    }

    /*
    设置默认地址
     */
    @RequestMapping("updataDefaultAddress")
    @ResponseBody
    @CrossOrigin
    public void updataDefaultAddress(String id){
        logger.debug("UseraddressController-updataDefaultAddress()-...start...id："+id);
        useraddressService.updataDefultAll();
        String success=useraddressService.updataDefaultAddress(id);
        logger.debug("UseraddressController-updataDefaultAddress()-success："+success);
    }

    /*
    根据id删除收货地址
     */
    @RequestMapping("deleteAddress")
    @ResponseBody
    @CrossOrigin
    public void deleteAddress(String id){
        logger.debug("UseraddressController-deleteAddress()-...start...id："+id);
        String success=useraddressService.deleteAddress(id);
        logger.debug("UseraddressController-deleteAddress()-success："+success);
    }

    /*
    当只有一条收货地址时，设置为默认地址
     */
    @RequestMapping("updataByUserId")
    @ResponseBody
    @CrossOrigin
    public void updataByUserId(){
        logger.debug("UseraddressController-updataByUserId()-...start...");
        //拿到存放在redis里面的登录用户
        String user=(String)redisTemplate.opsForValue().get("user");
        //把user Json字符串转换为user对象
        User userObj = JSONObject.parseObject(user, User.class);
        System.out.println("UseraddressController-updataByUserId()-userObj:"+userObj);
        //用户账号id
        int userId=userObj.getId();
       String success=useraddressService.updataByUserId(userId);
        logger.debug("UseraddressController-updataByUserId()-success："+success);
    }

    /*
    修改收货地址
     */
    @RequestMapping("updataAddress")
    @ResponseBody
    @CrossOrigin
    public String updataAddress(Useraddress useraddress){
        logger.debug("UseraddressController-updataAddress()-...start...useraddress："+useraddress);
        String success=useraddressService.updataAddress(useraddress);
        logger.debug("UseraddressController-updataAddress()-success："+success);
        return success;
    }

    /*
    根据id查询收货地址
     */
    @RequestMapping("getAddressById")
    @ResponseBody
    @CrossOrigin
    public Useraddress getAddressById(String id){
        logger.debug("UseraddressController-getAddressById()-...start...id："+id);
        Useraddress useraddress=useraddressService.getAddressById(id);
        logger.debug("UseraddressController-getAddressById()-useraddress："+useraddress);
        return useraddress;
    }

    /*
    查询一个登录账号有几个收货地址
     */
    @RequestMapping("getAddressCount")
    @ResponseBody
    @CrossOrigin
    public int getAddressCount(){
        logger.debug("UseraddressController-getAddressCount()-...start...");
        //拿到存放在redis里面的登录用户
        String user=(String)redisTemplate.opsForValue().get("user");
        //把user Json字符串转换为user对象
        User userObj = JSONObject.parseObject(user, User.class);
        System.out.println("UseraddressController-getAddressCount()-userObj:"+userObj);
        //用户账号id
        int userId=userObj.getId();
        logger.debug("UseraddressController-getAddressCount()-userId："+userId);
        int addressCount=useraddressService.getAddressCount(userId);
        logger.debug("UseraddressController-getAddressCount()-addressCount："+addressCount);
        return addressCount;
    }



}
